package com.ivi.car.module.carstate.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ivi.car.R;
import com.ivi.car.base.BaseFragment;
import com.ivi.car.bean.carstate.FaultDetial;
import com.ivi.car.bean.carstate.PressureDetial;
import com.ivi.car.module.carstate.model.IFaultDetialContract;
import com.ivi.car.module.carstate.presenter.FaultPresenter;
import com.wm.machine.baselibrary.common.widget.recyclerview.itemdecoration.SpaceItemDecoration;
import com.wm.machine.baselibrary.utils.LogUtils;
import com.wm.machine.baselibrary.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Copyright
 * author chengxp
 * date 2018/3/7
 * describe 故障提醒的页面
 * history
 */

public class FaultWarnFragment extends BaseFragment implements IFaultDetialContract.View {
    @BindView(R.id.tv_pressure_lf)
    TextView mTvPressureLf;
    @BindView(R.id.tv_tempature_lf)
    TextView mTvTempatureLf;
    @BindView(R.id.tv_pressure_rf)
    TextView mTvPressureRf;
    @BindView(R.id.tv_tempature_rf)
    TextView mTvTempatureRf;
    @BindView(R.id.tv_pressure_lr)
    TextView mTvPressureLr;
    @BindView(R.id.tv_tempature_lr)
    TextView mTvTempatureLr;
    @BindView(R.id.tv_pressure_rr)
    TextView mTvPressureRr;
    @BindView(R.id.tv_tempature_rr)
    TextView mTvTempatureRr;
    @BindView(R.id.iv_tyre_lf)
    ImageView mIvTyreLf;
    @BindView(R.id.iv_tyre_lr)
    ImageView mIvTyreLr;
    @BindView(R.id.iv_tyre_rf)
    ImageView mIvTyreRf;
    @BindView(R.id.iv_tyre_rr)
    ImageView mIvTyreRr;
    @BindView(R.id.rv_failure)
    RecyclerView mRvFailure;
    Unbinder unbinder;
    private FaultPresenter mFaultPresenter;
    private ArrayList<TextView> mPressureList;
    private ArrayList<TextView> mTemperatureList;
    private ArrayList<ImageView> mTyreList;
    private FaultAdapter mFaultAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_failure_warn, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mFaultPresenter = new FaultPresenter(this, null);
        mPressureList = new ArrayList<>();
        mTemperatureList = new ArrayList<>();
        mTyreList = new ArrayList<>();
        mPressureList.add(mTvPressureLf);
        mPressureList.add(mTvPressureRf);
        mPressureList.add(mTvPressureLr);
        mPressureList.add(mTvPressureRr);
        mTemperatureList.add(mTvTempatureLf);
        mTemperatureList.add(mTvTempatureRf);
        mTemperatureList.add(mTvTempatureLr);
        mTemperatureList.add(mTvTempatureRr);
        mTyreList.add(mIvTyreLf);
        mTyreList.add(mIvTyreRf);
        mTyreList.add(mIvTyreLr);
        mTyreList.add(mIvTyreRr);
        mFaultPresenter.showPressureDetial();
        mFaultPresenter.showFaultList();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void showPressureDetial(PressureDetial detial) {
        changePressureUI(detial.getPressurelf(), detial.getTemperaturelf(), 0);
        changePressureUI(detial.getPressurerf(), detial.getTemperaturerf(), 1);
        changePressureUI(detial.getPressurelr(), detial.getTemperaturelr(), 2);
        changePressureUI(detial.getPressurerr(), detial.getTemperaturerr(), 3);
    }

    private void changePressureUI(String pressure, String temperature, int inex) {
        TextView tvpre = mPressureList.get(inex);
        TextView tvtem = mTemperatureList.get(inex);
        ImageView iv = mTyreList.get(inex);
        int pre = Integer.parseInt(pressure);
        int tem = Integer.parseInt(temperature);
        tvpre.setText(pressure + "Kpa");
        tvtem.setText(temperature + "C");
        if (pre <= 300 || pre >= 310) {
            tvpre.setTextColor(ContextCompat.getColor(getActivity(), R.color.red));
        } else {
            tvpre.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
        }
        if (tem <= 20 || tem >= 25) {
            tvtem.setTextColor(ContextCompat.getColor(getActivity(), R.color.red));
        } else {
            tvtem.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
        }
        if (pre <= 300 || pre >= 310 || tem <= 20 || tem >= 27) {
            iv.setBackgroundResource(R.mipmap.icon_failure_tyre_warn);
        } else {
            iv.setBackgroundResource(R.mipmap.icon_failure_tyre_normal);
        }
    }

    @Override
    public void showFaultList(ArrayList<FaultDetial> list) {
        mFaultAdapter = new FaultAdapter(R.layout.item_fault_detial, list);
        mRvFailure.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvFailure.addItemDecoration(new SpaceItemDecoration(5, 5) );
        mRvFailure.setAdapter(mFaultAdapter);
    }

    private class FaultAdapter extends BaseQuickAdapter<FaultDetial, BaseViewHolder>{

        public FaultAdapter(int layoutResId, @Nullable List<FaultDetial> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, FaultDetial item) {
            LogUtils.e(item.getBtntext());
            helper.setText(R.id.tv_describe,item.getDesrcibe()).setText(R.id.btn_detial,item.getBtntext());
            if(StringUtils.isEmpty(item.getBtntext())){
                helper.setVisible(R.id.btn_detial,false);
            }else {
                helper.setVisible(R.id.btn_detial,true);
            }
        }
    }

}
